import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:situation/app/theme/app_theme.dart';
import 'package:situation/app/theme/gaps.dart';
import 'package:situation/resources/assets.dart';
import 'package:situation/shared/util/util_mk.dart';
import 'package:situation/widgets/app/app_title.dart';
import 'package:situation/widgets/map/layer/imaging_layer.dart';
import 'package:situation/widgets/mk_load_image.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'controller.dart';

class TaskDetailPage extends StatefulWidget {
  const TaskDetailPage({Key? key}) : super(key: key);

  @override
  State<TaskDetailPage> createState() => _TaskDetailPageState();
}

class _TaskDetailPageState extends State<TaskDetailPage> {
  final controller = Get.find<TaskDetailController>();
  final state = Get.find<TaskDetailController>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: TDNavBar(title: '任务详情'), body: _buildBody());
  }

  _buildBody() {
    return Stack(
      children: [
        Obx(
          () =>
              state.task.isEmpty == false
                  ? FlutterMap(
                    options: MapOptions(
                      initialCenter: LatLng(
                        double.parse(state.task['lat']),
                        double.parse(state.task['lon']),
                      ),
                      initialZoom: 17,
                      backgroundColor: Colors.black87,
                      keepAlive: false,
                    ),
                    children: [
                      ...ImagingLayer.getImagingLayer(),

                      MarkerLayer(
                        markers: [
                          Marker(
                            point: LatLng(
                              double.parse(state.task['lat']),
                              double.parse(state.task['lon']),
                            ),
                            child: MkLoadImage(Assets.mapTaskLocation),
                          ),
                        ],
                      ),
                    ],
                  )
                  : SizedBox(),
        ),
        Positioned(bottom: 0, child: _buildTaskDetail()),
      ],
    );
  }

  _buildTaskDetail() {
    return Obx(
      () => Container(
        width: Get.width,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(12),
            topRight: Radius.circular(12),
          ),
        ),
        padding: EdgeInsets.symmetric(
          horizontal: AppTheme.iDefaultPadding,
          vertical: 20,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Obx(
              () => Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  AppTitle(
                    title: state.task['isAccept'] == -1 ? '待接受' : '待完成',
                    icon: Assets.taskLocation,
                  ),
                  GestureDetector(
                    onTap: controller.toNavigator,
                    child: AppTitle(title: '导航', icon: Assets.taskLocation),
                  ),
                ],
              ),
            ),
            Gaps.vGap16,
            Text(
              "${state.task['title']}",
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            Gaps.vGap8,
            Row(
              children: [
                Text(
                  "${UtilMk.formatDateTimeYMDHIS(state.task['createTime'])}",
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(fontSize: 12),
                ),
                Gaps.hGap8,
                // TDTag(
                //   "${state.task['distance']}km",
                //   backgroundColor: Color(0xFFBDD2FD),
                //   textColor: Color(0xFF5B8FF9),
                //   size: TDTagSize.small,
                // ),
              ],
            ),
            Gaps.vGap8,
            Text(
              "${state.task['info']}",
              overflow: TextOverflow.ellipsis,
              maxLines: 2,
              style: TextStyle(fontSize: 12),
            ),
            Gaps.vGap8,
            AppTitle(
              title: "${state.task['address']}",
              textStyle: TextStyle(fontSize: 12, color: Color(0xff606266)),
              icon: Assets.taskLocation,
            ),
            Gaps.vGap16,
            Obx(()=>Row(
              children: [
                state.task['isAccept'] == "-1"
                    ? Expanded(
                  child: TDButton(
                    onTap: () {
                      controller.updateTask(TaskDetailStatus.REFUSE);
                    },
                    text: '拒绝',
                    theme: TDButtonTheme.danger,
                    shape: TDButtonShape.circle,
                    height: 42,
                    iconWidget: MkLoadImage(
                      Assets.taskOk,
                      width: 24,
                      height: 24,
                    ),
                  ),
                )
                    : SizedBox(),
                Gaps.hGap8,
                Expanded(
                  child: TDButton(
                    onTap: () {
                      state.task['isAccept'] == -1
                          ? controller.updateTask(TaskDetailStatus.ACCEPT)
                          : controller.updateTask(TaskDetailStatus.COMPLETE);
                    },
                    text: state.task['isAccept'] == -1 ? '去接受' : '已完成',
                    theme: TDButtonTheme.primary,
                    shape: TDButtonShape.circle,
                    height: 42,
                    iconWidget: MkLoadImage(
                      Assets.taskOk,
                      width: 24,
                      height: 24,
                    ),
                  ),
                ),
              ],
            )),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<TaskDetailController>();
    super.dispose();
  }
}
